package com.fymxy.question_bank_management.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fymxy.question_bank_management.pojo.ApiResult;
import com.fymxy.question_bank_management.pojo.ProblemWithBLOBs;
import com.fymxy.question_bank_management.service.KeywordService;
import com.fymxy.question_bank_management.service.serviceImpl.ProblemServiceImpl;
import com.fymxy.question_bank_management.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ProblemSelectController {
    @Autowired
    private ProblemServiceImpl problemService;
    @Autowired
    private KeywordService keywordService;

    @GetMapping("/beforeShowProblems")
    public ApiResult beforeShowProblems(){
        return ApiResultHandler.buildApiResult(200,"返回所有知识点",keywordService.getAllKeywordContent());
    }

    @GetMapping("/showProblems/{page}")
    public ApiResult showAllProblem(@PathVariable("page") Integer page){
        Page<ProblemWithBLOBs> problemWithBLOBsPage = new Page<>(page,10);
        IPage<ProblemWithBLOBs> allProblem= problemService.selectAllProblem(problemWithBLOBsPage);
        return ApiResultHandler.buildApiResult(200,"查询所有题目成功",allProblem);
    }

    @GetMapping("/selectProblems/{keyword}/{type}/{level}/{point}/{page}")
    public ApiResult selectProblem(@PathVariable("keyword") String keyword,@PathVariable("type") String type,@PathVariable("level") String level,
                                   @PathVariable("point") String point,@PathVariable("page") int page){
        Page<ProblemWithBLOBs>problemWithBLOBsPage = new Page<>(page,10);
        IPage<ProblemWithBLOBs>problemWithBLOBsIPage = problemService.selectByKeyword(problemWithBLOBsPage,keyword,level,type,point);
        return ApiResultHandler.buildApiResult(200,"查询指定题目成功",problemWithBLOBsIPage);
    }

    @GetMapping("/deleteProblem/{id}")
    public ApiResult deleteProblem(@PathVariable("id") Integer id){
        keywordService.deleteKeywordByProNo(id);
        return ApiResultHandler.buildApiResult(200,"删除题目成功",problemService.deleteProblemById(id));
    }

    @PostMapping("/updateProblem")
    public ApiResult updateProblem(@RequestBody ProblemWithBLOBs problemWithBLOBs){
        return ApiResultHandler.buildApiResult(200,"修改题目成功",problemService.updateProblemById(problemWithBLOBs));
    }

    @GetMapping("/selectProblem/{id}")
    public ApiResult selectProblem(@PathVariable("id") Integer id){
        return ApiResultHandler.buildApiResult(200,"选择成功",problemService.selectById(id));
    }
}
